﻿// ===============================================================================
// Checkbox.cs
// EOS Web Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Web.Mvc.Controls
{
    using System;
    using System.Globalization;
    using EOS.Framework.Tools;

    public class Checkbox : Control, IInputControl
    {
        private IForm _form;

        private string _name;

        public string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_name))
                {
                    return Key;
                }
                else
                {
                    return _name;
                }
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (StringHelper.IsEmptyOrBlanksOnly(value))
                {
                    throw new ArgumentException(Properties.Resources.EmptyStringParameter, "value");
                }

                _name = value;
            }
        }

        private bool _isChecked;

        public bool IsChecked
        {
            get { return _isChecked; }
            set { _isChecked = value; }
        }

        private string _inputValue;

        public string InputValue
        {
            get { return _inputValue; }
            set { _inputValue = value; }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBox"/> class.
        /// </summary>
        /// <param name="markup">The markup, where this control is assigned. Cannot be null.</param>
        /// <param name="isChecked">if set to <c>true</c> the checkbox will be checked.</param>
        /// <exception cref="ArgumentNullException"><paramref name="markup"/> is null (Nothing in Visual Basic).</exception>
        public Checkbox(IForm form)
            : this(form, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBox"/> class 
        /// with a name for the checkbox.
        /// </summary>
        /// <param name="markup">The markup, where this control is assigned. Cannot be null..</param>
        /// <param name="isChecked">if set to <c>true</c> the checkbox will be checked.</param>
        /// <param name="name">The name of the checkbox. Cannot be null.</param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="markup"/> is null (Nothing in Visual Basic).</para>
        /// <para>- oder -</para>
        /// <para><paramref name="name"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        /// <exception cref="ArgumentException"><paramref name="name"/> is a string of length 0 or 
        /// has only whitespaces.</exception>
        public Checkbox(IForm form, string name)
        {
            _name = name;
            _form = form;
            
        }

        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower().Equals("input") == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "input"));
            }

            if (!VerifyAttributeHasValue("type", "checkbox"))
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.WrongTagAttributeValue, "type", "checkbox"));
            }

            WriteBeginAttachedTag(writer);
            
            if (_isChecked)
            {
                writer.WriteAttribute("checked", "checked");
            }

            WriteEndAttachedTag(writer);
        }

        protected override void OnAttached()
        {
            if (_inputValue == null && _form != null)
            {
                _inputValue = _form.GetValue(Name);
            }

            if (Object.Equals(_inputValue, "on"))
            {
                _isChecked = true;
            }

            base.OnAttached();
        }

        public void Reset()
        {
            _isChecked = false;
            _inputValue = null;
        }
    }
}
